package socket;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 聊天室客户端
 */
public class Client {
    /*
        java.net.Socket 套接字
        封装了TCP协议的通讯细节，使用它可以和服务端进行可靠的网络传输通讯
     */
    private Socket socket;

    /**
     *  构造器，用于初始化客户端程序
     */
    public Client() {
        try {
            /*
                实例化Socket时通常传入两个参数
                参数1:服务端的IP地址，用于找到服务端的计算机
                参数2:端口号，用于找到运行在服务端计算机上的服务端程序里的ServerSocket(总机)
                并且，实例化的过程就是与服务端连接的过程
                注:"localhost"这个单词在IP地址中用于表示本机地址176.125.55.253
             */
            System.out.println("正在连接服务器...");
            socket = new Socket("localhost",8088);
            System.out.println("与服务器建立连接!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *  客户端开始工作的方法
     */
    public void start(){
        Scanner sc = new Scanner(System.in);
        try {
            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            ObjectOutputStream oos = new ObjectOutputStream(out);
//            while(true){
//                System.out.println("请输入昵称:");
//                String nickName = sc.nextLine();
//                if(nickName.trim().isEmpty()){
//                    System.out.println("昵称不能为空");
//                    continue;
//                }
//                pw.println(nickName);
//                break;
//            }
            System.out.println("请选择登录或注册账号");
            String line= sc.nextLine();
            pw.println(line);

            //用户登录或注册后获取UserInfo对象传输到服务端
            UserInfo ui = chooseLOS(line);
            if(ui != null){
                oos.writeObject(ui);
            }

            System.out.println("开始聊天:");

            Thread t = new Thread(new ServerHandler());
            t.setDaemon(true);
            t.start();

            while (true){
                line = sc.nextLine();
                if("exit".equals(line)) break;
                pw.println(line);
            }
            //out.write(1);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //close方法内部会与服务端进行四次挥手
                socket.close();//交互完毕后与服务端断开连接
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }

    private class ServerHandler implements Runnable{
        public void run(){
            try {
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                String line;
                while((line = br.readLine()) != null){
                    //读取服务端发送回来的一行字符串
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static UserInfo chooseLOS(String line){
        if ("注册".equals(line) || line == "注册") {
            UserInfo ui = InputUtil.getInputObject(new UserInfo(),"欢迎注册","注册");
            return ui;
        }else if("登录".equals(line) || line == "登录"){
            UserInfo ui = InputUtil.getInputObject(new UserInfo(),"欢迎登录","登录");
            return ui;
        }
        System.out.println("输入错误");
        return null;
    }
}
